package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.cache.TsCache;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Resource
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Resource
    private BaseAttributeMapper baseAttributeMapper;
    @Resource
    private BaseCategory3Mapper baseCategory3Mapper;

    @Override
    public List<JSONObject> getBaseCategoryList() {
        List<JSONObject> list = new ArrayList<>();
        //先查询出所有的分类数据再进行分组,这个数据是通过视图获取的
        //因为三级分类有三张表需要进行列表查询，而且数据变化不大采用视图更加方便
        List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
        //根据以及分类id进行分组，分组数据
        Map<Long, List<BaseCategoryView>> category1Map = baseCategoryViews.stream()
                .collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        //遍历map集合得到分组后的结果
        Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = category1Map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Long, List<BaseCategoryView>> next = iterator.next();
            Long category1Id = next.getKey();
            List<BaseCategoryView> baseCategoryViewList1 = next.getValue();
            JSONObject category1 = new JSONObject();
            category1.put("categoryId", category1Id);
            category1.put("categoryName", baseCategoryViewList1.get(0).getCategory1Name());
            Map<Long, List<BaseCategoryView>> category2Map = baseCategoryViewList1.stream()
                    .collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = category2Map.entrySet().iterator();
            List<JSONObject> category2List = new ArrayList<>();
            while (iterator1.hasNext()) {
                Map.Entry<Long, List<BaseCategoryView>> next1 = iterator1.next();
                Long category2Id = next1.getKey();
                List<BaseCategoryView> baseCategoryViewList2 = next1.getValue();
                JSONObject category2 = new JSONObject();
                category2.put("categoryId", category2Id);
                category2.put("categoryName", baseCategoryViewList2.get(0).getCategory2Name());
                //遍历得到三级分类
                List<JSONObject> jsonObjects = baseCategoryViewList2.stream().map(item -> {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("categoryName", item.getCategory3Name());
                    jsonObject.put("categoryId", item.getCategory3Id());
                    return jsonObject;
                }).toList();
                category2.put("categoryChild", jsonObjects);
                category2List.add(category2);
            }
            category1.put("categoryChild", category2List);
            list.add(category1);
        }
        return list;
    }

    @Override
    public List<BaseAttribute> findAttribute(Long category1Id) {
        //通过多表关联查询得到数据
        return baseAttributeMapper.findAttribute(category1Id);
    }

    @Override
    @TsCache(prefix = "category:")
    public BaseCategoryView getCategoryView(Long category3Id) {
        return baseCategoryViewMapper.selectById(category3Id);
    }

    @Override
    public List<BaseCategory3> findTopBaseCategory3ByCategory1Id(Long category1Id) {
        //	select * from base_category3 where base_category3.category2_id in (101,102,103) and is_top = 1 limit 7;
        //	先根据一级分类Id 找到二级分类集合
        return baseCategory3Mapper.findByCategory1Id(category1Id);
    }

    @Override
    public JSONObject getAllCategoryList(Long category1Id) {
        //声明一个一级分类对象
        JSONObject category1 = new JSONObject();
        //获取到一级分类对象
        List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper
                .selectList(new LambdaQueryWrapper<BaseCategoryView>()
                        .eq(BaseCategoryView::getCategory1Id, category1Id));
        category1.put("categoryId", category1Id);
        category1.put("categoryName", baseCategoryViews.get(0).getCategory1Name());
        Map<Long, List<BaseCategoryView>> category2Map = baseCategoryViews.stream()
                .collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
        Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = category2Map.entrySet().iterator();
        List<JSONObject> category2List = new ArrayList<>();
        while (iterator1.hasNext()) {
            Map.Entry<Long, List<BaseCategoryView>> next1 = iterator1.next();
            Long category2Id = next1.getKey();
            List<BaseCategoryView> baseCategoryViewList2 = next1.getValue();
            JSONObject category2 = new JSONObject();
            category2.put("categoryId", category2Id);
            category2.put("categoryName", baseCategoryViewList2.get(0).getCategory2Name());
            //遍历得到三级分类
            List<JSONObject> jsonObjects = baseCategoryViewList2.stream().map(item -> {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("categoryName", item.getCategory3Name());
                jsonObject.put("categoryId", item.getCategory3Id());
                return jsonObject;
            }).toList();
            category2.put("categoryChild", jsonObjects);
            category2List.add(category2);
        }
        category1.put("categoryChild", category2List);
        return category1;
    }

    @Override
    public List<BaseCategory1> findAllCategory1() {
        return this.baseMapper.selectList(null);
    }
}
